<template>
    <div class="common allianceCooperation">
        <innerCommon 
            :location="location"
        >
            <template slot="left">
                <span>活动列表</span>
                <a-input-search class="inputStyle" v-model="searchValue" placeholder="搜索活动标题名称" enter-button @search="onSearch" />
            </template>
            <div slot="right">
                <a-button type="primary" @click="add()">新增</a-button>
            </div>
            <div slot="table">
                <a-table 
                    size="middle"
                    :columns="columns" 
                    :data-source="list" 
                    :pagination="pagination"
                    :rowKey="record=>record.id"
                    @change="changePage"
                    :locale="{'emptyText': '暂无数据'}"
                    bordered>
                    <div slot="status" slot-scope="index,record">
                        <span v-if="record.status==1" style="color:green">已发布</span>
                        <span v-else style="color:red">未发布</span>
                    </div>
                    <div slot="action" slot-scope="index,record" class="tableAction">
                        <span @click="edit(record.id)">修改</span>
                        <confirmBox
                            @handleOk="deleteNews(record)"
                            :typeName="'删除'"
                            :noticeInfo="record.title"
                            :noticeType="'活动'"
                        ></confirmBox>
                        <confirmBox
                             v-if="record.status!=1"
                            @handleOk="publish(record.id)"
                            :typeName="'发布'"
                        ></confirmBox>
                        <confirmBox
                            @handleOk="stayAtTop(record.id,record.stayAtTop)"
                            :typeName="record.stayAtTop==0?'置顶':'取消置顶'"
                        ></confirmBox>
                    </div>
                </a-table>
            </div>
        </innerCommon>
    </div>
</template>
<script>
const columns=[
    {
        title: '序号',
        dataIndex: 'index',
        width:70
    },{
        title: '活动标题',
        dataIndex: 'title'
    },{
        title: '发布者',
        dataIndex: 'publisher'
    },{
        title: '发布时间',
        dataIndex: 'pubTime'
    },{
        title: '发布状态',
        scopedSlots: { customRender: 'status' },
    },{
        title: '操作',
        scopedSlots: { customRender: 'action' },
    }
]

import confirmBox from "@/components/common/confirmBox"
import innerCommon from "@/components/common/innerCommon"
import {cooperationPublishApi,cooperationStayTopApi,cooperationDeleteNewsApi,searchCooperationNewsApi} from "@/request/api"
export default {
    name:"allianceCooperation",
    components:{innerCommon,confirmBox},
    watch:{
        searchValue:function(val){
            this.searchValue=val.trim()
        }
    },
    created(){
        this.location=[{
            name:"产教融合",
            path:''
        },{
            name:"产教合作",
            path:'/allianceCooperation'
        }]
        this.init()
    },
    data(){
        return{
            columns,
            location:[],
            list:[],
            pagination:{pageSize:10,total:0,current:1},
            searchInfo:{
                page:1,
                pageSize:10,
                keyword:''
            },
            searchValue:''
        }
    },
    methods:{
        init(){
            this.getToken(searchCooperationNewsApi,this.searchInfo,res=>{
                this.dealData(res.data.rows,res.data.count)
            })
        },
        dealData(list,total){
            this.list=list.map((item,index)=>({...item,'index':(this.searchInfo.page-1)*10+index+1,'publisher':item.user&&item.user.name}))
            this.pagination.total=total
        },
        onSearch(data){
            this.searchInfo.keyword=data.trim()
            this.changePage({current:1})
        },
        changePage(data){
            this.searchInfo.page=data.current
            this.pagination.current=data.current
            this.init()
        },
        add(){
            this.$router.push({path:'/allianceCooperationAdd'})
        },
        publish(id){
            this.getToken(cooperationPublishApi,{'newsId':id},res=>{
                this.$message.success(res.message)
                this.init()
            })
        },
        stayAtTop(id,stayAtTop){
            this.getToken(cooperationStayTopApi,{'newsId':id,'stayAtTop':stayAtTop==0?1:0},res=>{
                this.$message.success(res.message)
                this.init()
            })
        },
        deleteNews(data){
            this.getToken(cooperationDeleteNewsApi,{'newsId':data.id},res=>{
                this.$message.success(res.message)
                this.init()
            })
        },
        edit(id){
            this.$router.push({path:'/allianceCooperationAdd',query:{id:id}})
        }
    }
}
</script>